package com.wgtgt.big_folder;

import java.io.File;

public class ScanForder {

	public static void scanFolder(String folder, long limit) {
		File folderAll = new File(folder);
		File[] listFiles = folderAll.listFiles();
		for (int i = 0; i < listFiles.length; i++) {
			File childFile = listFiles[i];
			if (childFile.isDirectory()) {
				long size = 0L;
				size = scan(childFile, size, limit);
				System.out.println("size=" + size + ",file=" + childFile);
			}
		}
	}

	private static long scan(File file, long size, long limit) {
		if (size > limit) {
			return size;
		}
		if (file.isFile()) {
			long len = size + file.length();
			return len;
		} else {
			File[] listFiles = file.listFiles();
			if (listFiles != null) {
				for (int i = 0; i < listFiles.length; i++) {
					File childFile = listFiles[i];
					size = scan(childFile, size, limit);
				}
			}
		}
		return size;
	}

}
